package com.ad.pet.ui.loopview.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ad.pet.R;
import com.ad.pet.common.data.utils.glide.GlideUtil;
import com.ad.pet.ui.loopview.entity.LoopViewEntity;

import java.util.List;

/**
 * Created by idisfkj on 16/8/8.
 * Email : idisfkj@qq.com.
 */
public class LoopViewAdapter extends PagerAdapter {
    private List<LoopViewEntity> list;
    private Context context;
    private OnItemClickListener listener;
    private final int MAX_SIZE = Integer.MAX_VALUE;
    private int defaultImageView;
    private int errorImageView;
    int type = 0;

    public LoopViewAdapter(Context context, List<LoopViewEntity> list) {
        this.context = context;
        this.list = list;
    }

    public LoopViewAdapter(Context context, List<LoopViewEntity> list, int type) {
        this.context = context;
        this.list = list;
        this.type = type;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
//        View view = LayoutInflater.from(context).inflate(type == 0 ? R.layout.loopview_item : R.layout.item_loop_roud, null);
        View view = LayoutInflater.from(context).inflate(R.layout.loopview_item,null);
        final ImageView imageView = view.findViewById(R.id.loop_image);

//        ImageView imageView = new ImageView(context);
////        imageView.setLayoutParams(new ViewGroup.LayoutParams(200, 180));
//        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onItemClick(position % list.size());
            }
        });
        container.addView(imageView);
//            GlideUtil.with(context).load(list.get(position % list.size()).getImageUrl())
//                    .placeholder(defaultImageView)
//                    .error(errorImageView)
//                    .into(imageView);
//        if (type == 0) {
        GlideUtil.loadNoDefault(context, list.get(position % list.size()).getImageUrl(), imageView);
//        } else {
//            GlideUtil.load(context, list.get(position % list.size()).getImageUrl(), imageView,R.color.bg_color,R.color.bg_color);
//        Glide.with(context).load(list.get(position % list.size()).getImageUrl()).into(imageView);
//        Glide.with(context)
//                .load(list.get(position % list.size()).getImageUrl())
//                .diskCacheStrategy(DiskCacheStrategy.NONE)
//                .override(800, 600)
//                .into(imageView);





        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//            if (isAnim){
//                container.removeAllViews();
//
//            }else {
        container.removeView((View) object);
//            }
        ((ViewGroup)((View) object).getParent()).removeView(((View) object));

    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setDefaultImageView(int defaultImageView) {
        this.defaultImageView = defaultImageView;
    }

    public void setErrorImageView(int errorImageView) {
        this.errorImageView = errorImageView;
    }


    private View mCurrentView;

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        mCurrentView = (View) object;
    }

    public View getPrimaryItem() {
        return mCurrentView;
    }


}
